package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/*
    菜单控制层
 */
@RestController
@RequestMapping("/menu")
public class MEnuController {

    @Reference
    private MenuService menuService;

    /*
        分页查询所有菜单
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询所有菜单列表
    @RequestMapping(value = "/findMenuAll")
    public Result findMenuAll(){
        try {
            List<Map<String, Object>> list = menuService.findMenuAll();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    /*
       查询所有一级菜单 用于回显到add菜单页面
    */
    @RequestMapping("/findFirstMenu")
    public Result findFirstMenu(){
        try {
            List<Map<String,Object>> list = menuService.findFirstMenu();
            return new Result(true,MessageConstant.QUERY_FIRST_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_FIRST_MENU_FAIL);
        }
    }

    /*
        添加菜单
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (java.lang.Exception exception) {
            exception.printStackTrace();
            return  new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    // deleteById 删除菜单
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    //findById 回显菜单信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /*
        修改菜单
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return  new Result(false,e.getMessage());
        }catch (Exception exception) {
            exception.printStackTrace();
            return  new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

}
